<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Column;
use addons\BuilderPlus\components\Edit;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;

class Demo extends Base
{
    public function getOptions(): array
    {
        return [
            //(必须)主键
            'pk' => 'id',
            //(必须)对应Model类
            'model' => \app\common\model\Account::class,

            //(可选)获取数据的url，默认为getList方法，建议不要改动
            'url' => url('getList')->build(),
            //(可选)对应验证类，只有新增时生效
            'validate' => \app\common\model\Account::class,
            //(可选)关联方法名
            'with' => ['user', 'take'],
            //(可选)附加字段
            'append' => ['statusText', 'typeText'],
            //(可选)导出表格式时，最大内存限制，默认最大1024M
            'memory_limit' => '1024M',
            //(可选)表单提交时，是否包含justData数据，默认true
            'just' => false,
            //(可选)快速定义方法，可节省代码行数，add，edit为请求的页面，其他均为post请求，对应model中的方法名
            'fast' => [
                'add' => 'draw/add',
                'edit' => 'draw/edit',
                'save' => '_save',
                'batch' => 'batch'
            ],
            //(可选)树形表格时，对应pid字段名
            'tree' => [
                'pid' => 'pid',
            ]
        ];
    }

    public function index(): string
    {
        return Table::init($this)
            ->addColumns([
                ['id', 'ID', fn(Column $col) => $col->width(80)],
                ['images', '缩略图', fn(Column $col) => $col->lazyImage()->width(100), fn(Edit $e) => $e->images()],
                ['title', '标题', null, fn(Edit $e) => $e->require()],
                ['sort', '排序', fn(Column $col) => $col->fastSort(), fn(Edit $e) => $e->number()],
                ['create_time', '添加时间', fn(Column $col) => $col->width(140)],
                ['status', '状态', fn(Column $col) => $col->switch('开启|关闭', '1|0')->fixed('right')]
            ])
            ->addTopButtons(['add', 'deleteAll'])
            ->addRightButtons(['edit', 'delete'])
            ->addSearch([
                ['create_time', '添加时间', fn(Search $search) => $search->type('date')],
                ['title', '标题', fn(Search $search) => $search->condition('Like')]
            ])
            ->fetch();
    }
}